package pl.auction_search.views;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.IViewPart;

import pl.auction_search.model.ISearchResultModel;
import pl.auction_search.model.ISearchResultModel.ISearchCategory;
import pl.auction_search.search.Result;

public class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider {
	protected IViewPart viewPart;
	private ISearchResultModel<Result> model;

	public ViewContentProvider(IViewPart viewPart, ISearchResultModel<Result> searchResultModel) {
		this.viewPart = viewPart;
		this.model = searchResultModel;
	}

	public Object[] getElements(Object parent) {
		if (model == null) return null;
		if (parent.equals(viewPart.getViewSite())) {
			return model.listCategories().toArray(new ISearchCategory<?>[] {});
		}
		return getChildren(parent);
	}

	public Object getParent(Object child) {
		if (model != null && child instanceof Result) {
			return model.getCategory(((Result) child).getCategory());
		}
		return null;
	}

	public Object[] getChildren(Object parent) {
		if (parent instanceof ISearchCategory<?>) {
			return ((ISearchCategory<?>) parent).toArray();
		}
		return new Object[0];
	}

	public boolean hasChildren(Object parent) {
		if (parent instanceof ISearchCategory<?>) {
			return ((ISearchCategory<?>) parent).list() != null;
		}
		return false;
	}

	@Override public void dispose() {
	}
	@Override public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
	}
}